package com.manager.core.module.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.manager.commons.annotation.Module;
import com.manager.commons.base.DataGrid;
import com.manager.core.module.manager.service.PlatformPermissionService;
import com.manager.core.module.manager.entity.PlatformPermission;
import com.manager.gen.web.controller.AdminController;
import com.manager.core.web.json.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 *
 * @author H
 * @version 1.0
 */
@Controller
@Module(parent = "basic-manager", title = "菜单管理",show = false,code = "/platform/menu")
public class PlatformPermissionController extends AdminController<PlatformPermission> {
    private final String baseUrl = "/platform/menu";
    @Autowired
    private PlatformPermissionService _service;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(baseUrl+"/index")
    public ModelAndView platformPermissionList() {
        return getModelAndView(baseUrl+"/menu-index");
    }

    /**
     * 分页
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/list", method = RequestMethod.POST)
    public @ResponseBody DataGrid platformPermissionListPage() {
        return _service.queryPage(getParams());
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(baseUrl+"/add")
    public ModelAndView add() {
        return getModelAndView(baseUrl + "/menu-edit", getModel());
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(baseUrl+"/edit")
    public ModelAndView platformUserEdit() {
        return getModelAndView(baseUrl + "/menu-edit", _service.findById(getIdParam()));
    }
    /**
     * 保存数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/save", method = RequestMethod.POST)
    public @ResponseBody AjaxJson platformPermissionSave() {
        return _service.save(getParams());
    }

    /**
     * 改变数据状态
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/status", method = RequestMethod.POST)
    public @ResponseBody AjaxJson platformPermissionStatus() {
        return _service.status(getParams());
    }

    /**
     * 删除数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/delete", method = RequestMethod.POST)
    public @ResponseBody AjaxJson platformPermissionDelete() {
        return _service.delete(getParams());
    }

    /**
     * 销毁数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/destroy", method = RequestMethod.POST)
    public @ResponseBody AjaxJson platformPermissionDestroy() {
        try {
            return _service.destroyAndRole(getParams());
        } catch (Exception e) {
            return new AjaxJson("异常错误，请稍后再试");
        }
    }
    /**
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/tree", method = RequestMethod.POST)
    public @ResponseBody JSONArray tree() {
        return JSON.parseArray(JSONArray.toJSONString(_service.queryTreeList()));
    }
    @Override
    public PlatformPermission getModel() {
        return new PlatformPermission();
    }
}